const express = require("express");
const db = require('../../utils/db')
const router = express.Router();
//初始化查询
router.post('/oneproject',(req,res)=>{
    const {createmen,title,startTime,state,page} = req.body;
    let xiuPage = (page-1)*5
    const mysql1 = `select count(*) as total from notice;`;
    const mysql = `select * from notice limit ${xiuPage},${page*5};`;
    console.log(page);
    db.query(mysql,(err,data)=>{
        if(err){
            res.send({error:err,msg:'错误'});
        }else{
            db.query(mysql1,(err1,data1)=>{
                res.send({'data':data,'total':data1});
            })
            
        }
    })
})
//修改
router.post('/upproject',(req,res)=>{
    const {id,title,conten,startTime,endTime,contenmen,state} =req.body
    //UPDATE 表名 SET 列1=值1，列2 = 值2 WHERE 条件；
    const mysql = `update notice set title='${title}',conten='${conten}',startTime='${startTime}',fabuTime='${endTime}',createmen='${contenmen}',state='${state}' where id=${id};`;
    db.query(mysql,(err,data)=>{
        if(err){
            res.send('错误');
        }else{
            res.send({msg:'ok','data':data});
        }
    })
})
//增加
router.post('/addproject',(req,res)=>{
    let {createmen,title,conton,startTime,state,fabuTime} =req.body
    //insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)
    let mysql;
    if(fabuTime){
        mysql = `insert into notice(title,conten,createmen,startTime,state,fabuTime) values ('${title}','${conton}',${createmen},'${startTime}',${state},'${fabuTime}') ;`;
    }else{
        mysql = `insert into notice(title,conten,createmen,startTime,state) values ('${title}','${conton}',${createmen},'${startTime}',${state}) ;`;
    }
    db.query(mysql,(err,data)=>{
        if(err){
            res.send({err:'错误'});
        }else{
            res.send({msg:'ok'});
        }
    })
});
//删除
router.post('/deleteproject',(req,res)=>{
    const {id} =req.body
    //delete from 数据表 where 条件表达式
    const mysql = `delete from notice where id=${id};`;
    db.query(mysql,(err,data)=>{
        console.log(data,err)
        if(err){
            res.send({err:'错误'});
        }else{
            res.send({msg:'ok'});
        }
    })
});
//发布
router.post('/upSta',(req,res)=>{
    const {id}=req.body;
    const mysql = `update notice set state=2 where id=${id};`
    db.query(mysql,(err,data)=>{
        console.log(data,err)
        if(err){
            res.send({err:'错误'});
        }else{
            res.send({msg:'ok'});
        }
    })
})
//撤回
router.post('/upFa',(req,res)=>{
    const {id}=req.body;
    const mysql = `update notice set state=3 where id=${id};`
    db.query(mysql,(err,data)=>{
        if(err){
            res.send({err:'错误'});
        }else{
            res.send({msg:'ok'});
        }
    })
})
// //发布
// router.post('/fasta',(req,res)=>{
//     const {id}=req.body;
//     const mysql = `update notice set state=1 where id=${id};`
//     db.query(mysql,(err,data)=>{
//         console.log(data,err)
//         if(err){
//             res.send({err:'错误'});
//         }else{
//             res.send({msg:'ok'});
//         }
//     })
// })
module.exports = router;